# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-dav
pkgver=2.0_git20240327
pkgrel=3
pkgdesc="CalDAV and CardDAV interface for grommunio"
# s390x: blocked by grommunio-gromox
arch="noarch !riscv64 !s390x"
url="https://grommunio.com/"
license="AGPL-3.0-only"
options="!check" # No test suite
_php=php83
install="$pkgname.pre-install $pkgname.pre-upgrade"
depends="grommunio-gromox
	grommunio-mapi-header-php
	$_php
	$_php-ctype
	$_php-curl
	$_php-dom
	$_php-iconv
	$_php-mbstring
	$_php-pdo_sqlite
	$_php-xml
	$_php-xmlreader
	$_php-xmlwriter
	"

_commithash=8133bc4a8d1a15011bc3d5c8de9a7a4a66191c3a

source="$pkgname-$pkgver.tar.gz::https://github.com/grommunio/grommunio-dav/archive/$_commithash.tar.gz
	$pkgname-$pkgver-vendor.tar.gz::https://nerd.hu/distfiles/grommunio-dav-$_commithash-vendor.tar.gz
	"

builddir="$srcdir/grommunio-dav-$_commithash/"

build() {
	:
}

package() {
	# webapp
	install -dm755 "$pkgdir"/usr/share/webapps/$pkgname/
	cp -rp -- * "$pkgdir"/usr/share/webapps/$pkgname/

	# vendor libraries
	cp -rp "$srcdir/vendor" "$pkgdir"/usr/share/webapps/$pkgname/

	# license
	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# nginx conf
	sed -i "s@/usr/share/grommunio-dav/@/usr/share/webapps/grommunio-dav/@" build/grommunio-dav.conf
	install -Dpm644 build/grommunio-dav.conf "$pkgdir"/usr/share/grommunio-common/nginx/locations.d/grommunio-dav.conf
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/grommunio-dav-upstream.conf
	install -Dpm644 build/grommunio-dav-upstream.conf "$pkgdir"/usr/share/grommunio-common/nginx/upstreams.d/grommunio-dav.conf

	# php-fpm
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/pool-grommunio-dav.conf
	install -Dpm644 build/pool-grommunio-dav.conf "$pkgdir"/etc/$_php/php-fpm.d/pool-grommunio-dav.conf

	# logrotate
	install -Dm644 build/grommunio-dav.lr "$pkgdir"/etc/logrotate.d/grommunio-dav

	# config
	install -Dm644 config.php "$pkgdir"/etc/grommunio-dav/config.php
	ln -sf "/etc/grommunio-dav/config.php" "$pkgdir"/usr/share/webapps/grommunio-dav/config.php
	install -Dm644 glogger.ini "$pkgdir"/etc/grommunio-dav/glogger.ini
	ln -sf "/etc/grommunio-dav/glogger.ini" "$pkgdir"/usr/share/webapps/grommunio-dav/glogger.ini

	# remove unnecessary files
	rm -r "$pkgdir"/usr/share/webapps/grommunio-dav/build
	rm -r "$pkgdir"/usr/share/webapps/grommunio-dav/tests
	rm "$pkgdir"/usr/share/webapps/grommunio-dav/LICENSE
	rm "$pkgdir"/usr/share/webapps/grommunio-dav/README.rst
	rm "$pkgdir"/usr/share/webapps/grommunio-dav/composer.*
}

sha512sums="
49603aae9f0a315f2b83e5efe33fba380b29f9b67016aa72647e78d4da2de5cdb4ca027b50252a85aaad5c2556904bf0d12d14709c9464acf066a9f6a2f1a5ba  grommunio-dav-2.0_git20240327.tar.gz
c3de362d7b34130e660c3bd7615dedbec106927f2d8931ee4e44c32eb62a0d17a6449322fd21780123168cb752ab209b752b8585d54d9c9492b556c12291b11e  grommunio-dav-2.0_git20240327-vendor.tar.gz
"
